/*
 * This file is part of "Sorting Thoughts" project.
 *
 * Copyright (c) 2009 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/sorting-thoughts/ for more information.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
SortingThoughts = {};

// GLOBALS
SortingThoughts.versionString = "1.3.0";
SortingThoughts.oldVersionString = "";
SortingThoughts.appName = $L("<strong>Sorting Thoughts&trade;</strong> for webOS V") + SortingThoughts.versionString;
SortingThoughts.copyright = $L("Copyright &copy; by Hendrik Ebel, 2010");
SortingThoughts.enableAppLock = false;
SortingThoughts.enableNewByShake = true;
SortingThoughts.showNewFirst = true; // sort by create (old naming)
SortingThoughts.sortByTitle = false;
SortingThoughts.sortByColor = false;
SortingThoughts.sortByVdate = false;
SortingThoughts.sortByMdate = false;
SortingThoughts.showDate = true;
SortingThoughts.firstStart = true;
SortingThoughts.dao = null;
SortingThoughts.dbErrorDetected = false;
SortingThoughts.istSicher = "Xhj8y23";
SortingThoughts.sicher = "wQ05poXl";
SortingThoughts.curLock = "";
SortingThoughts.curLockTwo = "";

SortingThoughts.MenuAttr = {
    omitDefaultItems: true
};

SortingThoughts.MenuAttrForRichText = {
	omitDefaultItems: true,
	richTextEditMenu: true
};

SortingThoughts.MenuModel = {
    visible: true,
    items: [ Mojo.Menu.editItem, {
        label: $L("Preferences"),
        command: "do-prefs"
    }, {
        label: $L("Help"),
        command: "do-help"
    }, ]
};

SortingThoughts.MenuModelEditView = {
    visible: true,
    items: [ Mojo.Menu.editItem, {
        label: $L("Preferences"),
		disabled: "true",
        command: "do-prefs"
    }, {
        label: $L("Help"),
		disabled: "true",
        command: "do-help"
    }, ]
};

SortingThoughts.SortMenuModel = {
    visible: true,
    items: [ Mojo.Menu.editItem, { 
		label: $L("Sorting"), items: [
				{ label: $L("Sort by title"), 				
				  command: 'sort-by-title' },	
				{ label: $L("Sort by creation date"),       	
				  command: 'sort-by-cdate' },
				{ label: $L("Sort by modification date"),   
				  command: 'sort-by-mdate' },
				{ label: $L("Sort by last view date"),   
				  command: 'sort-by-vdate' },
				{ label: $L("Sort by color"),       		
				  command: 'sort-by-color' },	
	]},{
        label: $L("Preferences"),
        command: "do-prefs"
    }, {
        label: $L("Help"),
        command: "do-help"
    }, ]
};

/**
 * Is the app running on a TouchPad?
 */
SortingThoughts.isTouchPad = function() {
	
	if(Mojo.Environment.DeviceInfo.modelNameAscii.indexOf("ouch") > -1) {
        return true;
	}

	if(Mojo.Environment.DeviceInfo.screenWidth == 1024){ 
		return true; 
	}

	if(Mojo.Environment.DeviceInfo.screenHeight == 1024){ 
		return true; 
	}

	return false;
};

function AppAssistant(appController){
}

AppAssistant.prototype.setup = function() {
	SortingThoughts.dao = new Dao();
    SortingThoughts.Cookie.initialize();
};

AppAssistant.prototype.handleCommand = function(event){
    var stageController = this.controller.getActiveStageController();
    var currentScene = stageController.activeScene();
    
    if (event.type == Mojo.Event.command) {
    
        switch (event.command) {
            case "do-prefs":
                stageController.pushScene("preferences");
                break;
                
            case "do-help":
			    stageController.pushScene("help");
                break;
			 case "sort-by-title":
			    SortingThoughts.sortByTitle = true;
				SortingThoughts.sortByColor = false;
				SortingThoughts.sortByMdate = false;
				SortingThoughts.sortByVdate = false;
				SortingThoughts.showNewFirst = false;
				SortingThoughts.Cookie.storeCookie();
				this.updateSettings();
				stageController.popScene("overview");
				stageController.pushScene("overview");
                break;
			 case "sort-by-cdate":
			    SortingThoughts.sortByTitle = false;
				SortingThoughts.sortByColor = false;
				SortingThoughts.sortByMdate = false;
				SortingThoughts.sortByVdate = false;
				SortingThoughts.showNewFirst = true;
				SortingThoughts.Cookie.storeCookie();
				this.updateSettings();
				stageController.popScene("overview");
				stageController.pushScene("overview");
                break;
			 case "sort-by-mdate":
			    SortingThoughts.sortByTitle = false;
				SortingThoughts.sortByColor = false;
				SortingThoughts.sortByMdate = true;
				SortingThoughts.sortByVdate = false;
				SortingThoughts.showNewFirst = false;
				SortingThoughts.Cookie.storeCookie();
				this.updateSettings();
				stageController.popScene("overview");
				stageController.pushScene("overview");
                break;
			 case "sort-by-color":
			    SortingThoughts.sortByTitle = false;
				SortingThoughts.sortByColor = true;
				SortingThoughts.sortByMdate = false;
				SortingThoughts.sortByVdate = false;
				SortingThoughts.showNewFirst = false;
				SortingThoughts.Cookie.storeCookie();
				this.updateSettings();
				stageController.popScene("overview");
				stageController.pushScene("overview");
                break;
			case "sort-by-vdate":
			    SortingThoughts.sortByTitle = false;
				SortingThoughts.sortByColor = false;
				SortingThoughts.sortByMdate = false;
				SortingThoughts.sortByVdate = true;
				SortingThoughts.showNewFirst = false;
				SortingThoughts.Cookie.storeCookie();
				this.updateSettings();
				stageController.popScene("overview");
				stageController.pushScene("overview");
                break;
        }
    }
    
};

AppAssistant.prototype.updateSettings = function() {
	Mojo.Log.info("Update settings");
	var now = new Date();
    var localOffset = now.getTimezoneOffset() * 60000;
	var sortingOrder = SortingThoughts.dao.getCurrentSortingOrderName();
	
	SortingThoughts.dao.updateSettings(localOffset, sortingOrder);
};
